package com.driver.core.mapper.generator;

import com.driver.core.Column;
import com.driver.core.StringUtil;
import com.driver.core.Table;
import com.driver.core.mapper.annotation.IMapper;
import com.driver.core.mapper.annotation.Order;
import org.apache.ibatis.annotations.Param;


public class MapperGenerator implements IMapper {
    private Table table;
    private String privateKey = "Id";
    private String type = "Integer";
    private String lowerClassName;

    public MapperGenerator(Table table) {
        this.table = table;
        for (Column column : table.getColumns()) {
            if (column.isPrivateKey()) {
                privateKey = column.getPropertyName();
                type = column.getPropertyType();
            }
        }

        lowerClassName = StringUtil.lowerFirstName(table.getClassName());
    }

    public MapperGenerator() {
    }

    @Override
    @Order(order = 1)
    public String selectCount() {
        return "\n\t" + "int selectCount(" + table.getClassName() + " " + StringUtil.lowerFirstName(table.getClassName() + ")" + ";\n");
    }

    @Override
    @Order(order = 5)
    public String selectById() {
        return "\n\t" + table.getClassName() + " selectBy" + StringUtil.upperFirstName(privateKey) + "(" + "@Param(\"" + privateKey + "\") " + type + " " + privateKey + ");\n";
    }


    @Override
    @Order(order = 10)
    public String selectOne() {
        return "\n\t" + table.getClassName() + " selectOne(" + table.getClassName() + " " + StringUtil.lowerFirstName(table.getClassName() + ")" + ";\n");
    }

    @Override
    @Order(order = 20)
    public String selectColumnNameByOne() {
        return "\n\t" + table.getClassName()  + " selectColumnNameByOne(@Param(\"" + StringUtil.lowerFirstName(table.getClassName()) + "Condition\") " + table.getClassName() + " " + StringUtil.lowerFirstName(table.getClassName()) + "Condition " + ",@Param(\"columnName\") String columnName);\n";

    }




    @Override
    @Order(order = 30)
    public String selectByList() {
        return "\n\tList<" + table.getClassName() + ">" + " selectByList(" + table.getClassName() + " " + StringUtil.lowerFirstName(table.getClassName() + ")" + ";\n");
    }

    @Override
    @Order(order = 40)
    public String selectColumnNameByList() {
        return "\n\tList<" + table.getClassName() + ">" + " selectColumnNameByList(@Param(\"" + StringUtil.lowerFirstName(table.getClassName()) + "Condition\") " + table.getClassName() + " " + StringUtil.lowerFirstName(table.getClassName()) + "Condition " + ",@Param(\"columnName\") String columnName);\n";

    }




    @Override
    @Order(order = 50)
    public String selectByMaps() {
        return "\n\tList<Map<String, Object>>" + " selectByMaps(" + table.getClassName() + " " + StringUtil.lowerFirstName(table.getClassName() + ")" + ";\n");
    }


    @Override
    @Order(order = 60)
    public String selectColumnNameByMaps() {
        return "\n\tList<Map<String, Object>>" + " selectColumnNameByMaps(@Param(\"" + StringUtil.lowerFirstName(table.getClassName()) + "Condition\") " + table.getClassName() + " " + StringUtil.lowerFirstName(table.getClassName()) + "Condition " + ",@Param(\"columnName\") String columnName);\n";
    }

    @Override
    @Order(order = 70)
    public String insert() {

        return "\n\tint insert(" + table.getClassName() + " " + StringUtil.lowerFirstName(table.getClassName()) + ");\n";
    }

    @Override
    @Order(order = 80)
    public String insertAllColumn() {

        return "\n\tint insertAllColumn(" + table.getClassName() + " " + StringUtil.lowerFirstName(table.getClassName()) + ");\n";
    }

    @Override
    @Order(order = 90)
    public String updateAllColumn() {
        String lowerFirstname = StringUtil.lowerFirstName(table.getClassName());
        return "\n\tint updateAllColumn(@Param(\"" + lowerFirstname + "Condition\") " + table.getClassName() + " " + StringUtil.lowerFirstName(table.getClassName()) + "Condition" + ", " + "@Param(\"" + lowerFirstname + "\") " + table.getClassName() + " " + lowerFirstname + ");\n";
    }

    @Override
    @Order(order = 100)
    public String update() {
        return "\n\tint update(@Param(\"" + lowerClassName + "Condition\") " + table.getClassName() + " " + lowerClassName + "Condition" + ", " + "@Param(\"" + lowerClassName + "\") " + table.getClassName() + " " + lowerClassName + ");\n";
    }

    @Override
    @Order(order = 110)
    public String updateById() {
        return "\n\tint updateBy" + StringUtil.upperFirstName(privateKey) + "(@Param(\"" + privateKey + "\") " + type + " " + privateKey + ", @Param(\"" + lowerClassName + "\") " + table.getClassName() + " " + lowerClassName + ");\n";
    }

    @Override
    @Order(order = 120)
    public String deleteById() {

        return "\n\tint deleteBy" + StringUtil.upperFirstName(privateKey) + "(" + "@Param(\"" + privateKey + "\") " + type + " " + privateKey + ");\n";
    }

    @Override
    @Order(order = 130)
    public String delete() {
        return "\n\tint delete(" + table.getClassName() + " " + lowerClassName + ");\n";
    }

    @Override
    @Order(order = 140)
    public String selectBySql() {
        return "\n\tList<Map<String, Object>> selectBySql(@Param(\"sql\") String sql);\n";
    }

}
